<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class auth extends MX_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('auth_model');
    }

    public function index()
    {
        $this->load->view('users/login');
    }

    public function login()
    {
        $this->index();
    }

    public function doLogin()
    {
        $username = trim($this->input->post("USR"));
        $pass = imd5($this->input->post("PWD"));

        if ($this->auth_model->isLocked($username)) {
            notify_set_messages(t('Username has locked!'));
            return redirect(site_url('auth'));
        }
        if (!$this->auth_model->checkUSR($username, $pass)) {
            notify_set_messages(t('Email or Password not valid'), 'auth');
            return redirect(site_url('auth'));
        } else {
            $this->auth_model->addSession($username);
            redirect(base_url());
        }
    }

    public function logout()
    {
        unset($_SESSION["CI_LOGIN"]);
        $this->session->sess_destroy();
        //$this->session->unset_userdata(array('auth'));
        redirect(site_url("auth/login"));
    }
}
